package com.iona.servlets;

import static java.lang.Integer.valueOf;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.iona.dao.PartDisStudyWorkDAO;
import com.iona.domain.BaseEntity;
import com.iona.domain.PartDisStudyWork;
import com.iona.json.JSONException;
import com.iona.utils.CreatorUtil;
import com.iona.utils.Storage;
/**
 * Servlet implementation class AddPartDisStudyWork
 */
@WebServlet("/AddPartDisStudyWorkServlet")
public class AddPartDisStudyWorkServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	@Override
	public void init() throws ServletException {
		super.init();
		setTransactional(true);
	}

	@Override
	public void doExecute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException,
			JSONException {
		String pdid = request.getParameter("pdid");
		String swid = request.getParameter("swid");
		if (pdid == null || swid == null) {
			goToList(request, response);
			return;
		}
		Integer pDisId = valueOf(pdid);
		Integer stWId = valueOf(swid);
		PartDisStudyWork pdsw = new PartDisStudyWorkDAO()
				.findByPartDisAndStudyWork(pDisId, stWId);

		if (pdsw == null) {
			PartDisStudyWork createPDSW = CreatorUtil.createPDSW(pDisId, stWId);
			List<BaseEntity> toCreate = new ArrayList<BaseEntity>();
			toCreate.add(createPDSW);
			new Storage().storeAll(toCreate, null, null);
		}
		goToList(request, response);
		
	}

	

}
